package org.tcit.erp.commons.utils;

/**
 * 分页类
 * 
 * @author wangj
 * 
 */
public class Page {
	/**
	 * 当前页
	 */
	private int currPage;

	/**
	 * 每页大小
	 */
	private int pageSize;

	/**
	 * 总记录
	 */
	private int allSize;

	

	/**
	 * 开始位置
	 */
	private int beginIndex;

	/**
	 * 结束位置
	 */
	private int endIndex;

	/**
	 * 总页数
	 */
	private int countPage;

	/**
	 * 是否是最后一页
	 */
	private boolean isEndPage;

	/**
	 * 是否是第一页
	 */
	private boolean isStartPage;

	/**
	 * @param currPage
	 *            当前页
	 * @param pageSize
	 *            每页大小
	 */
	public Page(int currPage, int pageSize) {
		this.currPage = currPage;
		this.pageSize = pageSize;
		

	}

	public void init(){
		// 计算总页数
		if ((this.allSize % this.pageSize) == 0) {
			this.countPage = this.allSize / this.pageSize;
		} else {
			this.countPage = this.allSize / this.pageSize + 1;
		}
		// 计算开始位置
		this.beginIndex = (this.currPage - 1) * this.pageSize;
		// 当开始位置大于总数时 取最后一页
		if (this.beginIndex >= this.allSize) {
			this.beginIndex = (this.countPage - 1) * this.pageSize;
			this.currPage=this.countPage;
			this.isEndPage = true;
		}else{
			//当开始位置小于0时 取第一页
			if(this.beginIndex<=0){
				this.beginIndex=0;
				this.currPage=1;
				this.isStartPage=true;
			} 
			 
		}
		
		//计算结束位置
		this.endIndex=this.currPage*this.pageSize;
		
		//设置最后一页
		if(this.endIndex>=this.allSize){
			this.isEndPage=true;
		}
		
	}
	
	public static void main(String[] args) {
		Page page=new Page(1, 5);
		page.setAllSize(20).init();
		 
		System.out.println("当前第几页："+page.getCurrPage());
		System.out.println("总共有多少页:"+page.getCountPage());
		System.out.println("开始位置:"+page.getBeginIndex());
		System.out.println("结束位置:"+page.getEndIndex());
		System.out.println("是否是第一页:"+page.isStartPage());
		System.out.println("是否是最后一页:"+page.isEndPage());
	}
	
	public int getCurrPage() {
		return currPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public int getAllSize() {
		return allSize;
	}

	public int getBeginIndex() {
		return beginIndex;
	}

	public int getEndIndex() {
		return endIndex;
	}

	public int getCountPage() {
		return countPage;
	}

	public boolean isEndPage() {
		return isEndPage;
	}

	public boolean isStartPage() {
		return isStartPage;
	}
	public Page setAllSize(int allSize) {
		this.allSize = allSize;
		return this;
	}

}
